package com.jbl.te.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.te.entity.College;
import com.jbl.te.entity.School;
import com.jbl.te.entity.vo.CollegeQuery;
import com.jbl.te.entity.vo.CollegeVO;
import com.jbl.te.service.CollegeService;
import com.jbl.te.service.SchoolService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/te/college")
@CrossOrigin
public class CollegeController {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private SchoolService schoolService;

    @GetMapping("/getAllCollege")
    @ApiOperation("获取所有学院")
    public R getAllCollege() {
        return R.ok().data("list",collegeService.list());
    }

    @PostMapping("{page}/{limit}")
    @ApiOperation("分页学院列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) CollegeQuery collegeQuery) {
        Page<College> pageParam = new Page<>(page, limit);
        collegeService.pageQuery(pageParam, collegeQuery);
        List<College> records = pageParam.getRecords();
        List<CollegeVO> list = new ArrayList<>();
        for (College college : records) {
            CollegeVO vo = new CollegeVO();
            BeanUtils.copyProperties(college, vo);
            School school = schoolService.getById(college.getSchoolId());
            vo.setSchoolName(school.getName());
            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }
    @PostMapping("saveCollege")
    @ApiOperation("添加学院(TODO)")
    public R saveCollege(@RequestBody College college) {

        boolean b = collegeService.save(college);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }
    @GetMapping("getCollegeById")
    @ApiOperation("根据id查找学院")
    public R getCollegeById(String id) {
        College college = collegeService.getById(id);
        if(college == null) {
            return R.error().message("学院不存在");
        }
        CollegeVO vo = new CollegeVO();
        BeanUtils.copyProperties(college,vo);
        School school = schoolService.getById(college.getSchoolId());
        vo.setSchoolName(school.getName());
        return R.ok().data("college",vo);
    }
    @PutMapping("updateCollege")
    @ApiOperation("修改学院")
    public R updateTeacher(@RequestBody College college) {
        boolean b = collegeService.saveOrUpdate(college);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("修改失败");
        }
    }
    @DeleteMapping("delCollegeById")
    @ApiOperation("删除学院")
    public R delCollegeById(String id) {
        //删除功能 TODO
        //return R.error().message("暂不支持删除功能");
        boolean b = collegeService.removeById(id);
        if(b) {

           return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }
    @DeleteMapping("delCollegeByList")
    @ApiOperation("批量删除学院")
    public R delTeachersByList(@RequestBody List<String> idList) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//        boolean b = collegeService.removeByIds(idList);
//        if(b) {
////            return R.ok();
//        }
//        return R.error().message("批量删除失败");
    }
}

